# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jshin

import abc
import logging

from coin.exchange.base.order_gateway import OrderGatewayBase


class CancelAllExecutorInterface(metaclass=abc.ABCMeta):
  def cancel_all_on_exit(self, ogs):
    ogs_initialized = list(filter(None.__ne__, ogs))
    assert all([isinstance(og, OrderGatewayBase) for og in ogs_initialized])
    return self.cancel_all_on_exit_impl(ogs_initialized)

  @abc.abstractmethod
  def cancel_all_on_exit_impl(self, ogs):
    raise NotImplementedError()


class SimpleCancelAllExecutor(CancelAllExecutorInterface):
  def __init__(self, logger=None):
    self._logger = logger or logging.getLogger('SimpleCancelAllExecutor')

  def cancel_all_on_exit_impl(self, ogs):
    finished = True
    for og in ogs:
      if og.get_working_order():
        self._logger.error('Cancel all. Exchange: %s' % og.exchange)
        og.cancel_all()
        finished = False
    return finished
